/**
 * @project Textoo
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @timestamp 26/09/2011 08:54
 * @version 1.0
 */

package  
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import net.hires.debug.Stats;
	import org.looty.textoo.*;
	import org.looty.textoo.context.*;
	import org.looty.textoo.events.*;
	import org.looty.textoo.format.*;
	import uk.co.soulwire.gui.*;
	
	public class Demo extends Sprite
	{
		
		/** 
		* Embed Font
		*/
		[Embed(source="Vera.ttf", mimeType="application/x-font-truetype", fontName="Bitstream Vera Sans", fontStyle="normal", fontWeight="normal", unicodeRange="U+0020-U+007E,U+00A1-U+00FF,U+0152-U+0153", embedAsCFF='false')]
		static public const VERA:Class;
		
		[Embed(source="VeraBd.ttf", mimeType="application/x-font-truetype", fontName="Bitstream Vera Sans", fontStyle="normal", fontWeight="bold", unicodeRange="U+0020-U+00FF,U+0152-U+0153", embedAsCFF='false')]
		static public const VERA_BOLD:Class;
		
		
		/** Embed Binary */
		[Embed(source="../bin/css/latin.css", mimeType="application/octet-stream")]
		static public const LATIN:Class;
		
		/** Embed Binary */
		[Embed(source="../bin/css/devices.css", mimeType="application/octet-stream")]
		static public const DEVICES:Class;
		
		/** Embed Binary */
		[Embed(source="../bin/xml/lang/ru.xml", mimeType="application/octet-stream")]
		static public const RU:Class;
		
		/** Embed Binary */
		[Embed(source="../bin/xml/lang/fr.xml", mimeType="application/octet-stream")]
		static public const FR:Class;
		
		/** Embed Binary */
		[Embed(source="../bin/xml/lang/ar.xml", mimeType="application/octet-stream")]
		static public const AR:Class;
		
		/** Embed Binary */
		[Embed(source="../bin/xml/lang/en.xml", mimeType="application/octet-stream")]
		static public const EN:Class;
		
		/** Embed Binary */
		[Embed(source="../bin/xml/lang/es.xml", mimeType="application/octet-stream")]
		static public const ES:Class;
		
		private var _column1	:Textoo;
		private var _column2	:Textoo;
		private var _column3	:Textoo;
		
		private var _distance	:Number;
		
		private var _context	:TextooContext; 
		
		private var _gui		:SimpleGUI;
		
		private var _cWidth		:Number;
		private var _cHeight	:Number;
		
		public function Demo() 
		{
			_distance = 0;
			_cWidth = 200;
			_cHeight = 250;
			initialise();
			addControls();
		}
		
		private function initialise():void
		{
			_context = new TextooContext();
			addLanguage("en", EN);
			addLanguage("ar", AR);
			addLanguage("es", ES);
			addLanguage("fr", FR);
			addLanguage("ru", RU);
			
			var CSS:String = String(new LATIN());
			var CSS2:String = String(new DEVICES());
			
			_context.addStyleSheet(CSS, "en");
			_context.addStyleSheet(CSS, "es");
			_context.addStyleSheet(CSS, "fr");
			_context.addStyleSheet(CSS2, "ar");
			_context.addStyleSheet(CSS2, "ru");
			
			Textoo.defineGlobalContext(_context);
			
			_column1 = new Textoo(200, 250, "udhr");
			_column1.x = 70;
			_column1.y = 200;
			_column1.bitmapLetterClass = MovingBitmapLetter;
			_column1.textFieldLetterClass = MovingTextFieldLetter;
			//_column1.addEventListener(TextooEvent.UPDATE, updateTextoo);
			addChild(_column1);
			
			_column2 = new Textoo(200, 250);
			_column2.x = 290;
			_column2.y = 200;
			_column2.bitmapLetterClass = MovingBitmapLetter;
			_column2.textFieldLetterClass = MovingTextFieldLetter;
			//_column2.addEventListener(TextooEvent.UPDATE, updateTextoo);
			addChild(_column2);
			
			_column3 = new Textoo(200, 250);
			_column3.x = 510;
			_column3.y = 200;
			_column3.bitmapLetterClass = MovingBitmapLetter;
			_column3.textFieldLetterClass = MovingTextFieldLetter;
			_column3.addEventListener(TextooEvent.UPDATE, updateTextoo);
			addChild(_column3);
			
			_column1.link(_column2);
			_column2.link(_column3);
		}
		
		private function addControls():void
		{		
			_gui = new SimpleGUI(this, "Textoo");
			_gui.show();
			_gui.addColumn("language");
			_gui.addComboBox("context.language", [ { label:"Arabic",	data:"ar" }, { label:"English",	data:"en" }, { label:"Spanish",	data:"es" }, { label:"French",	data:"fr" }, { label:"Russian",	data:"ru" } ]);			
			_gui.addButton("detect language", { callback : context.detectLanguage } );
			_gui.addSlider("columnWidth", 200, 250);
			_gui.addSlider("columnHeight", 250, 400);
			_gui.addColumn("render");
			_gui.addToggle("useDeviceFonts");
			_gui.addToggle("smoothing");
			_gui.addSlider("sharpness", -400, 400);
			_gui.addSlider("thickness", -200, 200);
			_gui.addColumn("rasterize & split");
			_gui.addComboBox("textColorType", [{ label : "Light Color", data:TextColorType.LIGHT_COLOR }, { label : "Dark Color", data:TextColorType.DARK_COLOR}]);
			_gui.addToggle("isBitmap");
			_gui.addToggle("isSplitted");			
			_gui.addSlider("distance", 0, 15);		
			
			var stats:Stats = new Stats();
			addChild(stats);
			stats.x = stage.stageWidth - stats.width;
		}
		
		private function updateTextoo(e:TextooEvent):void 
		{	
			applyDistance();
			
			_column1.x = 385 - _cWidth * 1.5;
			_column2.x = 400 - _cWidth * .5;
			_column3.x = 415 + _cWidth * .5;
		}		
		
		private function applyDistance():void
		{
			if (!isSplitted) return;
			var letter:IMovingLetter;
			var sq:Number = _distance * _distance
			
			for each (letter in _column1.letters) if (letter != null) letter.distance = sq;
			for each (letter in _column2.letters) if (letter != null) letter.distance = sq;
			for each (letter in _column3.letters) if (letter != null) letter.distance = sq;
		}		
		
		private function addLanguage(code:String, xmlClass:Class):void 
		{
			var xml:XML = XML(new xmlClass());			
			for each (var text:XML in xml.text) _context.addLocalText(text.attribute("key")[0], code, String(text), text.attribute("class")[0]);
		}
		
		public function get context():TextooContext 
		{
			return _context;
		}
		
		public function set isBitmap(value:Boolean):void
		{
			_column1.isBitmap = value;
			_column2.isBitmap = value;
			_column3.isBitmap = value;
		}
		
		public function get isBitmap():Boolean
		{
			return _column1.isBitmap;
		}
		
		public function set smoothing(value:Boolean):void
		{
			_column1.smoothing = value;
			_column2.smoothing = value;
			_column3.smoothing = value;
		}
		
		public function get smoothing():Boolean
		{
			return _column1.smoothing;
		}
		
		public function set useDeviceFonts(value:Boolean):void
		{
			_column1.useDeviceFonts = value;
			_column2.useDeviceFonts = value;
			_column3.useDeviceFonts = value;
		}
		
		public function get useDeviceFonts():Boolean
		{
			return _column1.useDeviceFonts;
		}
		
		public function set isSplitted(value:Boolean):void
		{
			_column1.isSplitted = value;
			_column2.isSplitted = value;
			_column3.isSplitted = value;
		}
		
		public function get isSplitted():Boolean
		{
			return _column1.isSplitted;
		}
		
		public function set textColorType(value:String):void
		{
			_column1.textColorType = value;
			_column2.textColorType = value;
			_column3.textColorType = value;
		}
		
		public function get textColorType():String
		{
			return _column1.textColorType;
		}
		
		public function set sharpness(value:Number):void
		{
			_column1.sharpness = value;
			_column2.sharpness = value;
			_column3.sharpness = value;
		}
		
		public function get sharpness():Number
		{
			return _column1.sharpness;
		}
		
		public function set thickness(value:Number):void
		{
			_column1.thickness = value;
			_column2.thickness = value;
			_column3.thickness = value;
		}
		
		public function get thickness():Number
		{
			return _column1.thickness;
		}
		
		public function set distance(value:Number):void
		{			
			_distance = value;
			applyDistance();
		}
		
		public function get distance():Number
		{
			return _distance;
		}
		
		public function set columnWidth(value:Number):void
		{
			_column1.width = value;
			_column2.width = value;
			_column3.width = value;
			_cWidth = value;
		}
		
		public function get columnWidth():Number
		{
			return _cWidth;
		}
		
		public function set columnHeight(value:Number):void
		{
			_column1.height = value;
			_column2.height = value;
			_column3.height = value;
			_cHeight = value;
		}
		
		public function get columnHeight():Number
		{
			return _cHeight;
		}
		
	}

}